package com.hxwang.tanke;

import java.awt.*;
import java.util.Random;

import static com.hxwang.tanke.Bullet.*;


/**
 * <H2> 坦克类 </H2>
 *
 * @author hxwang
 * @data 2022/12/4
 */
public class Tank {
    private int x; // 坐标
    private int y; // 坐标
    private int WIDTH, HEIGHT;
    private int speed = 10; // 速度
    private Dir dir = Dir.DOWN; // 方向
    private boolean moving = false;// 是否移动
    private boolean living = true; // 是否活着
    private Group group;// 区分敌我

    private Random random = new Random();

    public Rectangle rectangle = new Rectangle();

    public static final int TWIDTHL = ResoourcesMgr.tankL.getWidth();
    public static final int THEIGHTL = ResoourcesMgr.tankL.getHeight();

    public static final int TWIDTHR = ResoourcesMgr.tankR.getWidth();
    public static final int THEIGHTR = ResoourcesMgr.tankR.getHeight();

    public static final int TWIDTHU = ResoourcesMgr.tankU.getWidth();
    public static final int THEIGHTU = ResoourcesMgr.tankU.getHeight();

    public static final int TWIDTHD = ResoourcesMgr.tankD.getWidth();
    public static final int THEIGHTD = ResoourcesMgr.tankD.getHeight();

    private TankFrame tf;

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public boolean isMoving() {
        return moving;
    }

    public Tank(int x, int y, int speed, Dir dir, Group group, TankFrame tf) {
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.dir = dir;
        this.tf = tf;
        this.group = group;

        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.width = WIDTH;
        rectangle.height = HEIGHT;
    }


    public Tank() {
    }

    public void paint(Graphics g) {
//        g.fillRect(x, y, 50, 50);
        if (!living) {
            tf.tanks.remove(this);
        }
        Color c = g.getColor();
        switch (dir) {
            case LEFT:
                g.drawImage(this.group == Group.GOOD ? ResoourcesMgr.mTankL : ResoourcesMgr.tankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.GOOD ? ResoourcesMgr.mTankR : ResoourcesMgr.tankR, x, y, null);
                break;
            case UP:
                g.drawImage(this.group == Group.GOOD ? ResoourcesMgr.mTankU : ResoourcesMgr.tankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.GOOD ? ResoourcesMgr.mTankD : ResoourcesMgr.tankD, x, y, null);
                break;
        }
        g.setColor(c);
        move();
    }

    // 根据坦克的方向让其移动
    private void move() {
        // 如果坦克是敌方坦克默认会自动移动
        if (this.group == Group.BAD) this.moving = true;
        if (!moving) return;
        switch (dir) {
            case LEFT:
                x -= speed;
                rectangle.width = TWIDTHL;
                rectangle.height = THEIGHTL;
                break;
            case UP:
                y -= speed;
                rectangle.width = TWIDTHU;
                rectangle.height = THEIGHTU;
                break;
            case RIGHT:
                x += speed;
                rectangle.width = TWIDTHR;
                rectangle.height = THEIGHTR;
                break;
            case DOWN:
                y += speed;
                rectangle.width = TWIDTHD;
                rectangle.height = THEIGHTD;
                break;
        }
        // update rectangle
        rectangle.x = this.x;
        rectangle.y = this.y;
        // 敌方坦克随机开火
        if (this.group == Group.BAD && random.nextInt(100) < 3) {
            this.fire(this.group);
        }
        // 敌方坦克随机改变方向
        if (this.group == Group.BAD && random.nextInt(100) < 5) {
            randomDir();
        }
        // 边界检测
        boundCheck();
    }

    private void boundCheck() {
        if (this.y < 30) y = 30;
        if (this.x < 0) x = 0;
        if (this.y > TankFrame.GAME_HEIGHT - THEIGHTD) y = TankFrame.GAME_HEIGHT - THEIGHTD;
        if (this.x > TankFrame.GAME_WIDTH - TWIDTHR) x = TankFrame.GAME_WIDTH - TWIDTHR;
    }

    /**
     * 随机给坦克一个方向
     *
     * @author hxwang
     * @date 2022/12/31
     */
    private void randomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getSpeed() {
        return speed;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public Dir getDir() {
        return dir;
    }

    public void fire(Group group) {
        switch (dir) {
            case LEFT:
                tf.bullets.add(new Bullet(x - BWIDTHL, y + (THEIGHTL >> 1) - (BHEIGHTR >> 1), this.dir, group, tf));
                break;
            case RIGHT:
                tf.bullets.add(new Bullet(x + TWIDTHR, y + (THEIGHTR >> 1) - (BHEIGHTR >> 1), this.dir, group, tf));
                break;
            case UP:
                tf.bullets.add(new Bullet(x + (TWIDTHU >> 1) - (BWIDTHU >> 1), y - BHEIGHTU, this.dir, group, tf));
                break;
            case DOWN:
                tf.bullets.add(new Bullet(x + (TWIDTHD >> 1) - (BWIDTHU >> 1), y + THEIGHTD, this.dir, group, tf));
                break;
        }
    }

    public void die() {
        this.living = false;
    }
}